* ==============================================================================
*
* Date:	Summer 2024
* Paper: Climate shocks, regional favoritism and trust in leaders: Insights from droughts in Africa
*
* This creates Table 5
*
* ==============================================================================

* Open dataset
cd $PATH
use tr_rep, clear

* Global macros

* Control sets
global xcont "age age2 male i.ed_ord"

global fecontc "urban ea_svc_a ea_svc_b ea_svc_c ea_fac_a ea_fac_b ea_fac_c ea_fac_d ea_fac_e ea_sec_a ea_sec_b grs0 grsem0 grsl cropland_d ln_alt ln_dist_cit ln_alt_sd ln_dist_bor ln_dist_coa ln_dist_riv ln_dist_roa i.year i.month i.uid0 countrytrend*"

*su $xcont $fecontc if id0==84

* Clustering level
global clustervar "cidgrid01"

* Clean sample
keep if precision_code<=3
global zxcont "age age2 male ed_ord"
global zfecont "urban ea_svc_a ea_svc_b ea_svc_c ea_fac_a ea_fac_b ea_fac_c ea_fac_d ea_fac_e ea_sec_a ea_sec_b grs0 grsem0 grsl cropland_d ln_alt ln_dist_cit ln_alt_sd ln_dist_bor ln_dist_coa ln_dist_riv ln_dist_roa"

foreach var of varlist $zxcont $zfecont {
	drop if `var'==.
}

* Standardize
egen z=std(aiclwmcgrwsp)
replace aiclwmcgrwsp=z
drop z

* Interaction terms
gen inter1=aiclwmcgrwsp*capregion
label var inter1 "Capital region X Drought"
gen inter2=aiclwmcgrwsp*presregion
label var inter2 "LBR X Drought"

label var aicasp12 "AI of SPEI-12 at capital location"
label var aiprsp12 "AI of SPEI-12 at leader birth place location"

* Table name
global tabname "caplbr"

* Regression(s)
timer clear
local x=0
local z = ""

*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aiclwmcgrwsp $xcont $fecontc, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'

*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aiclwmcgrwsp capregion $xcont $fecontc, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'

*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aiclwmcgrwsp capregion inter1 $xcont $fecontc, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'


*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aicasp12 $xcont $fecontc if capregion==0, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'

*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aiclwmcgrwsp presregion $xcont $fecontc, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'

*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aiclwmcgrwsp presregion inter2 $xcont $fecontc, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'

*c
local x=`x'+1
di "Column `x'"
timer on `x'
qui reg tr_pres_ord aiprsp12 $xcont $fecontc if presregion==0, vce(cluster $clustervar)
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'


* Post-table 
timer list   
di "`z'"

* Simplified table generation
global keepvars "aiclwmcgrwsp capregion inter1 aicasp12 presregion inter2  aiprsp12"

* In Stata output window
esttab `z' , ///
	mtitles("All" "All" "All" "Outside Capital Region" "All" "All" "Outside LBR") ///
	addnotes("Dependent variable: Trust in President" "Standard errors clustered on 0.1-degree grid cells" "Country FE, not Region FE, in this table") ///
	keep($keepvars) ///
	order($keepvars) ///
	cells(b(star fmt(%8.3f) ) se( par fmt(%6.3f)) ) ///
	stats(N r2, fmt(%9.0f %9.2f)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) r2(3) ///
	legend


* Excel-file
estout `z' using $OUTPATH/$tabname.xls, replace ///
	mlabels("All" "All" "All" "Outside Capital Region" "All" "All" "Outside LBR") ///
	postfoot("Dependent variable: Trust in President" "Standard errors clustered on 0.1-degree grid cells" "Country FE, not Region FE, in this table") ///
	keep($keepvars) ///
	order($keepvars) ///
	cells(b(star fmt(%8.3f) ) se( par(`"="("' `")""') fmt(%6.3f)) ) ///
	stats(N r2, fmt(%9.0f %9.2f)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	legend


